<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isShowDialog = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <!-- 以 data 传入数组,数组里面是一行行的数据对象
            column 组件是一列, prop 指定这个列显示哪个字段 -->
            <el-table border="" :data="roleList">
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template v-slot="obj">
                  <el-button size="small" type="success" @click="assignPerm(obj.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(obj.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(obj.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :page-size="pageSetting.pagesize"
                :total="total"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 弹窗组件 -->
      <!-- title 属性决定了弹窗的标题
      visible 属性是布尔值, 决定了弹窗是否显示 -->

      <el-dialog :title="formData.id?'编辑角色':'新增角色'" :visible="isShowDialog" @close="btnCancel">
        <el-form ref="addForm" :model="formData" :rules="rules" label-width="78px">
          <el-form-item label="角色名" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </template>
      </el-dialog>

      <el-dialog title="分配权限" :visible="isShowPerm" @close="btnPermCancel">
        <el-checkbox-group v-model="checkList">
          <!-- <el-checkbox v-for="perm in permList" :key="perm.id" :label="perm.id">
            {{ perm.name }}
          </el-checkbox> -->
          <el-checkbox v-for="perm in permList" :key="perm.id" :label="perm.id">
            {{ perm.name }}
          </el-checkbox>
          <!-- 自己遍历没法形成树形, 所以借助 el-tree
          el-tree 的作用域插槽替换了原来自己遍历 v-for 的 perm 树形 -->
          <el-tree
            :data="permList"
            :default-expand-all="true"
            :expand-on-click-node="false"
          >
            <template v-slot="obj">
              <el-checkbox :label="obj.data.id">
                {{ obj.data.name }}
              </el-checkbox>
            </template>
          </el-tree>
        </el-checkbox-group>

        <template #footer>
          <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getCompanyInfo,
  getRoleList,
  addRole,
  getRoleDetail,
  editRole,
  delRole,
  assignPerm
} from '@/api/setting'
import { getPermList } from '@/api/permission'
import { listToTreeData } from '@/utils'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      // 是否显示设置角色权限弹窗
      isShowPerm: false,
      // 全部权限列表
      permList: [],
      // 已经选中的权限列表
      checkList: [],
      roleId: '',
      // 公司信息对象
      companyInfo: {},
      // 角色列表数组
      roleList: [],
      // 获取角色列表时翻页设定
      pageSetting: {
        page: 1,
        pagesize: 2
      },
      // 总条数
      total: 0,
      isShowDialog: false,
      formData: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getCompanyInfo()
    this.getRoleList()
    this.getPermList()
  },
  methods: {
    async getPermList() {
      this.permList = listToTreeData(await getPermList(), '0')
      console.log(this.permList)
    },
    async assignPerm(roleId) {
      // 准备弹出分配权限弹窗之前
      // 获取点击角色的数据, 将原有权限放入自动显示
      this.roleId = roleId
      const res = await getRoleDetail(roleId)
      // 其中原权限数据放在 res.permIds 里面
      this.checkList = res.permIds
      console.log(res)
      this.isShowPerm = true
    },
    async btnPermOK() {
      // 发请求
      await assignPerm({
        id: this.roleId,
        permIds: this.checkList
      })
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.isShowPerm = false
    },
    btnPermCancel() {
      // 清理数据
      this.checkList = []
      // 关闭弹窗
      this.isShowPerm = false
    },
    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.companyId)
    },
    async getRoleList() {
      const res = await getRoleList(this.pageSetting)
      this.roleList = res.rows
      this.total = res.total
    },
    currentChange(newPage) {
      this.pageSetting.page = newPage
      this.getRoleList()
    },
    async editRole(id) {
      this.formData = await getRoleDetail(id)
      this.isShowDialog = true
      console.log('表格点击按钮,作用域插槽传入被点击角色行的 id', id)
    },
    async delRole(id) {
      try {
        // 二次确认
        await this.$confirm('是否确认删除?')
        // 发送请求
        await delRole(id)
        // 提示用户
        this.$message.success('操作成功')
        // 更新页面
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    async btnOK() {
      try {
        // 校验表单
        await this.$refs.addForm.validate()
        // 发送请求
        if (this.formData.id) {
          // 编辑
          await editRole(this.formData)
        } else {
          // 新增
          await addRole(this.formData)
        }
        // 关闭弹窗
        this.isShowDialog = false
        // 提醒用户
        this.$message.success('操作成功')
        // 更新页面
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      // 清理数据
      // 清理校验
      // 这个数据清理, 清理报错非常好用
      // 但是清理数据有局限, 只会恢复一开始有绑定的数据
      this.$refs.addForm.resetFields()
      this.formData = {
        name: '',
        description: ''
      }
      // 关闭弹窗
      this.isShowDialog = false
    }
  }
}
</script>

<style>

</style>
